package com.yunquan.ohana.ui.calendar.task.filter

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.lifecycle.LifecycleOwner
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.binding.command.BindingConsumer
import com.yunquan.ohana.base.bus.event.SingleLiveEvent
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.RoleEntity
import com.yunquan.ohana.entity.TaskFilterDto
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.calendar.profiles.add.AddProfilesActivity
import com.yunquan.ohana.ui.calendar.task.TaskRoleItemViewModel
import io.reactivex.rxjava3.disposables.Disposable
import me.tatarka.bindingcollectionadapter2.ItemBinding
import okhttp3.RequestBody.Companion.toRequestBody
import kotlin.collections.forEach

class TaskFilterViewModel(application: Application) : BaseViewModel(application) {

    val ui = SingleLiveEvent<Int>()
    val completed = ObservableField(true)
    val lateChores = ObservableField(true)

    val items = ObservableArrayList<TaskFilterRoleItemViewModel>()
    val itemBinding = ItemBinding.of<TaskFilterRoleItemViewModel>(
        BR.taskFilterRoleItemViewModel,
        R.layout.item_filter_role,
    )

    var mDeviceId: String? = null
    var mUserType: Boolean = false
    var mFilterBean: TaskFilterDto? = null

    val onAddProfilesClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("deviceId", mDeviceId)
            startActivityResult(AddProfilesActivity::class.java, bundle)
        }
    })

    val onContinueClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (mFilterBean == null) {
                mFilterBean = TaskFilterDto(
                    completed.get() ?: true,
                    lateChores.get() ?: true,
                    items.filter { it.checked.get() == true }.map { it.data }
                )
            } else {
                mFilterBean!!.completed = completed.get() ?: true
                mFilterBean!!.lateChores = lateChores.get() ?: true
                mFilterBean!!.roleList = items.filter { it.checked.get() == true }.map { it.data }
            }
            ui.value = 0
        }
    })

    fun getProfilesList() {
        val map = mutableMapOf<String, Any>()
        map["deviceId"] = mDeviceId!!
        val body = Gson().toJson(map).toRequestBody(BaseHttp.MEDIA_TYPE_JSON)
        BaseHttp.api.getDeviceRoleListById(body)
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<List<RoleEntity>>() {
                override fun onSubscribe(d: Disposable) {
                    super.onSubscribe(d)
                    showLoading()
                }

                override fun onSuccess(result: List<RoleEntity>?) {
                    super.onSuccess(result)
                    items.clear()
                    result?.forEach {
                        items.add(
                            TaskFilterRoleItemViewModel(
                                this@TaskFilterViewModel,
                                it,
                                edit = if (mUserType) {
                                    true
                                } else {
                                    it.type == 1
                                },
                                check = mFilterBean?.roleList?.any { role -> it.id == role.id } == true
                            )
                        )
                    }
                }

                override fun onComplete() {
                    super.onComplete()
                    hideLoading()
                }
            })
    }
}